import { req_login } from '@/api/user'
import { getProfileAPI } from '@/api/permiss'
const TOKEN_KEY = 'smart_park_token'
export default {
  namespaced: true,
  state:() => {
    return {
      token: localStorage.getItem(TOKEN_KEY) || '',
      profile:{},
    }
  },
  mutations: {
    //保存用户token
    setUserToken(state,token){
      state.token = token
      localStorage.setItem(TOKEN_KEY, token)
    },
    //清除用户信息
    clearUserInfo(state){
      state.token = ''
      state.profile = {}
      localStorage.removeItem(TOKEN_KEY)
    },
    setProfile(state, profile) {
      state.profile = profile
    } 
  },
  actions: {
    async loginAction(context,userInfo){
     let res = await req_login(userInfo)
      context.commit('setUserToken',res.data.token)
    },
    async getProfile(ctx) {
      const res = await getProfileAPI()
      ctx.commit('setProfile', res.data)

       // return后，可以直接在  const perms = await store.dispatch('user/getUserProfile')
      // 时接收到返回的数据
      return res.data.permissions 
    }

  }
}
